<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>reader.c</title>
<style type="text/css">
<!--
body.hl	{ background-color:#ffffff; }
pre.hl	{ color:#000000; background-color:#ffffff; font-size:10pt; font-family:'Courier New';}
.hl.num { color:#ff0000; }
.hl.esc { color:#ff22ff; }
.hl.str { color:#ff0000; }
.hl.dstr { color:#ff0000; }
.hl.slc { color:#0000ff; }
.hl.com { color:#0000ff; }
.hl.dir { color:#ff22ff; }
.hl.sym { color:#000000; }
.hl.line { color:#0000ff; }
.hl.mark	{ background-color:#ffffbb;}
.hl.kwa { color:#b26818; }
.hl.kwb { color:#00ff00; }
.hl.kwc { color:#b26818; }
.hl.kwd { color:#000000; }
//-->
</style>
</head>
<body class="hl">
<pre class="hl"><span class="hl com">/*</span>
<span class="hl com"> *  reader.c</span>
<span class="hl com"> *  scheme2</span>
<span class="hl com"> *</span>
<span class="hl com"> *  Created by Leo Katzenstein on 5/25/07.</span>
<span class="hl com"> *  Copyright 2007 __MyCompanyName__. All rights reserved.</span>
<span class="hl com"> *</span>
<span class="hl com"> */</span>
<span class="hl dir"># include</span> <span class="hl dstr">&quot;scheme.h&quot;</span><span class="hl dir"></span>

<span class="hl com">/* enum Token1 { LEFT1, RIGHT1, tNUM, tSYM }; */</span>
<span class="hl kwb">char</span> buf<span class="hl sym">[</span><span class="hl num">500</span><span class="hl sym">];</span>
Pair <span class="hl sym">*</span>atom<span class="hl sym">;</span>   <span class="hl com">/* when the token is an atom, this will be set */</span>

<span class="hl com">/* nyi -- not yet implimented error message</span>
<span class="hl com">          does not return</span>
<span class="hl com"> */</span>
<span class="hl kwb">void</span> <span class="hl kwd">nyi</span><span class="hl sym">(</span> <span class="hl kwb">char</span> <span class="hl sym">*</span>what <span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwd">fprintf</span><span class="hl sym">(</span> stderr<span class="hl sym">,</span> <span class="hl str">&quot;%s is not yet implemented.</span><span class="hl esc">\n</span><span class="hl str">Exiting.</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl sym">,</span> what <span class="hl sym">);</span>
    <span class="hl kwd">exit</span><span class="hl sym">(</span> <span class="hl num">3</span> <span class="hl sym">);</span>
<span class="hl sym">}</span>

<span class="hl kwb">void</span> <span class="hl kwd">error</span><span class="hl sym">(</span> <span class="hl kwb">char</span> <span class="hl sym">*</span>msg <span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwd">fprintf</span><span class="hl sym">(</span> stderr<span class="hl sym">,</span> <span class="hl str">&quot;Error: %s</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl sym">,</span> msg <span class="hl sym">);</span>
    <span class="hl kwd">exit</span><span class="hl sym">(</span> <span class="hl num">1</span> <span class="hl sym">);</span>
<span class="hl sym">}</span>

<span class="hl kwb">int</span> <span class="hl kwd">is_white</span><span class="hl sym">(</span> <span class="hl kwb">int</span> ch <span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwa">if</span> <span class="hl sym">(</span> ch <span class="hl sym">==</span> <span class="hl str">' '</span> <span class="hl sym">||</span> ch <span class="hl sym">==</span> <span class="hl str">'</span><span class="hl esc">\n</span><span class="hl str">'</span> <span class="hl sym">||</span> ch <span class="hl sym">==</span> <span class="hl str">'</span><span class="hl esc">\t</span><span class="hl str">'</span> <span class="hl sym">)</span>
        <span class="hl kwa">return</span> TRUE<span class="hl sym">;</span>
    <span class="hl kwa">else</span>
        <span class="hl kwa">return</span> FALSE<span class="hl sym">;</span>
<span class="hl sym">}</span>

<span class="hl kwb">int</span> <span class="hl kwd">is_break</span><span class="hl sym">(</span> <span class="hl kwb">int</span> ch <span class="hl sym">)</span>
<span class="hl sym">{</span>
    <span class="hl kwa">if</span> <span class="hl sym">(</span> ch <span class="hl sym">==</span> EOF <span class="hl sym">||</span>
         ch <span class="hl sym">==</span> <span class="hl str">'('</span> <span class="hl sym">||</span>
         ch <span class="hl sym">==</span> <span class="hl str">')'</span> <span class="hl sym">||</span>
         <span class="hl kwd">is_white</span><span class="hl sym">(</span> ch <span class="hl sym">) )</span>
        <span class="hl kwa">return</span> TRUE<span class="hl sym">;</span>
    <span class="hl kwa">else</span>
        <span class="hl kwa">return</span> FALSE<span class="hl sym">;</span>
<span class="hl sym">}</span>


<span class="hl kwb">int</span> <span class="hl kwd">gettok</span><span class="hl sym">()</span>
<span class="hl sym">{</span>
    <span class="hl com">/* int ch; */</span>
    <span class="hl kwb">int</span> n<span class="hl sym">;</span>
    <span class="hl kwb">int</span> isnum<span class="hl sym">;</span>
    <span class="hl kwb">int</span> token<span class="hl sym">;</span>

    n <span class="hl sym">=</span> <span class="hl num">0</span><span class="hl sym">;</span>
    isnum <span class="hl sym">=</span> TRUE<span class="hl sym">;</span>

    token <span class="hl sym">=</span> <span class="hl kwd">yylex</span><span class="hl sym">();</span>

    <span class="hl kwa">return</span> token<span class="hl sym">;</span>
<span class="hl dir">#ifdef NOTDEF</span>
    <span class="hl com">/* skip white space */</span>
    <span class="hl kwa">while</span> <span class="hl sym">(</span> <span class="hl kwd">is_white</span><span class="hl sym">(</span> ch <span class="hl sym">=</span> <span class="hl kwd">getc</span><span class="hl sym">(</span>f<span class="hl sym">) ))</span>
        <span class="hl sym">;</span>

    <span class="hl com">/* we're looking at a nonwhite char */</span>
    <span class="hl kwa">if</span> <span class="hl sym">(</span> ch <span class="hl sym">==</span> EOF <span class="hl sym">)</span>
        <span class="hl kwa">return</span> EOF<span class="hl sym">;</span>
    <span class="hl kwa">if</span> <span class="hl sym">(</span> ch <span class="hl sym">==</span> <span class="hl str">'('</span> <span class="hl sym">)</span>
        <span class="hl kwa">return</span> LEFT<span class="hl sym">;</span>
    <span class="hl kwa">if</span> <span class="hl sym">(</span> ch <span class="hl sym">==</span> <span class="hl str">')'</span> <span class="hl sym">)</span>
        <span class="hl kwa">return</span> RIGHT<span class="hl sym">;</span>

    <span class="hl kwa">do</span> <span class="hl sym">{</span>
        buf<span class="hl sym">[</span> n<span class="hl sym">++ ] =</span> ch<span class="hl sym">;</span>
        <span class="hl kwa">if</span> <span class="hl sym">( !</span><span class="hl kwd">isdigit</span><span class="hl sym">(</span> ch <span class="hl sym">) )</span>
            isnum <span class="hl sym">=</span> FALSE<span class="hl sym">;</span>
    <span class="hl sym">}</span> <span class="hl kwa">while</span> <span class="hl sym">( !</span> <span class="hl kwd">is_break</span><span class="hl sym">(</span> ch <span class="hl sym">=</span> <span class="hl kwd">getc</span><span class="hl sym">(</span> f <span class="hl sym">) ) );</span>
    buf<span class="hl sym">[</span>n<span class="hl sym">] =</span> <span class="hl str">'\0'</span><span class="hl sym">;</span>  <span class="hl com">/* terminate the string */</span>
    <span class="hl kwd">ungetc</span><span class="hl sym">(</span> ch<span class="hl sym">,</span> f <span class="hl sym">);</span>
    <span class="hl kwa">if</span> <span class="hl sym">(</span> isnum <span class="hl sym">)</span>
    <span class="hl sym">{</span>
        atom <span class="hl sym">=</span> <span class="hl kwa">new</span><span class="hl sym">(</span> Pair <span class="hl sym">);</span>
        atom<span class="hl sym">-&gt;</span>car <span class="hl sym">= (</span>Pair <span class="hl sym">*)</span> NUMBER<span class="hl sym">;</span>
        atom<span class="hl sym">-&gt;</span>cdr <span class="hl sym">= (</span>Pair <span class="hl sym">*)</span> <span class="hl kwd">atoi</span><span class="hl sym">(</span> buf <span class="hl sym">);</span>
        <span class="hl kwa">return</span> tNUM<span class="hl sym">;</span>
    <span class="hl sym">}</span>
    <span class="hl kwa">else</span>
    <span class="hl sym">{</span>
        atom <span class="hl sym">=</span> <span class="hl kwd">make_sym</span><span class="hl sym">(</span> <span class="hl kwd">strdup</span><span class="hl sym">(</span> buf <span class="hl sym">) );</span>
        <span class="hl kwa">return</span> tSYM<span class="hl sym">;</span>
    <span class="hl sym">}</span>
<span class="hl dir">#endif</span>
<span class="hl sym">}</span>

<span class="hl com">/* read an expression */</span>
Pair <span class="hl sym">*</span><span class="hl kwd">readexp</span><span class="hl sym">()</span>
<span class="hl sym">{</span>
    <span class="hl kwb">enum</span> Token t<span class="hl sym">;</span>
    <span class="hl kwb">char</span> str<span class="hl sym">[</span><span class="hl num">80</span><span class="hl sym">];</span>

    <span class="hl kwa">switch</span> <span class="hl sym">( (</span>t <span class="hl sym">=</span> <span class="hl kwd">gettok</span><span class="hl sym">()) )</span>
    <span class="hl sym">{</span>
        <span class="hl kwa">case</span> <span class="hl num">0</span><span class="hl sym">:</span>
            <span class="hl kwa">return</span> <span class="hl sym">(</span>Pair <span class="hl sym">*)</span> EOF<span class="hl sym">;</span>

        <span class="hl kwa">case</span> LEFT<span class="hl sym">:</span>
            <span class="hl kwa">return</span> <span class="hl kwd">readlist</span><span class="hl sym">();</span>

        <span class="hl kwa">case</span> RIGHT<span class="hl sym">:</span>
            <span class="hl com">/* nyi( &quot;RIGHT&quot; );   &lt;= this is the key !! */</span>
            <span class="hl kwa">return</span> <span class="hl sym">(</span>Pair <span class="hl sym">*)</span> RIGHT<span class="hl sym">;</span>

        <span class="hl kwa">case</span> tSYM<span class="hl sym">:</span>
            <span class="hl kwa">return</span> <span class="hl kwd">make_sym</span><span class="hl sym">(</span>yytext<span class="hl sym">);</span>

        <span class="hl kwa">case</span> tFIXNUM<span class="hl sym">:</span>
            <span class="hl kwa">return</span> <span class="hl kwd">make_num</span><span class="hl sym">(</span><span class="hl kwd">atol</span><span class="hl sym">(</span>yytext<span class="hl sym">));</span>

        <span class="hl kwa">case</span> tFLOAT<span class="hl sym">:</span>
            <span class="hl kwa">return</span> <span class="hl kwd">make_double</span><span class="hl sym">(</span><span class="hl kwd">atof</span><span class="hl sym">(</span>yytext<span class="hl sym">));</span>

        <span class="hl kwa">case</span> tQUOTE<span class="hl sym">:</span>
            <span class="hl kwa">return</span> <span class="hl kwd">cons</span><span class="hl sym">(</span>quote<span class="hl sym">,</span> <span class="hl kwd">cons</span><span class="hl sym">(</span><span class="hl kwd">readexp</span><span class="hl sym">(),</span> NULL<span class="hl sym">));</span>

        <span class="hl kwa">default</span><span class="hl sym">:</span>
            <span class="hl kwd">sprintf</span><span class="hl sym">(</span> str<span class="hl sym">,</span> <span class="hl str">&quot;unrecognized token type %d&quot;</span><span class="hl sym">,</span> t <span class="hl sym">);</span>
            <span class="hl kwd">error</span><span class="hl sym">(</span> str <span class="hl sym">);</span>
    <span class="hl sym">}</span>
    <span class="hl kwd">error</span><span class="hl sym">(</span> <span class="hl str">&quot;internal error - should never get here&quot;</span> <span class="hl sym">);</span>
    <span class="hl kwa">return</span> NULL<span class="hl sym">;</span>
<span class="hl sym">}</span>

<span class="hl com">/* readlist - read a list</span>
<span class="hl com"> * This has been called after the opening left paren has been seen</span>
<span class="hl com"> */</span>
Pair <span class="hl sym">*</span><span class="hl kwd">readlist</span><span class="hl sym">()</span>
<span class="hl sym">{</span>
    Pair list_head<span class="hl sym">;</span>
    Pair <span class="hl sym">*</span>tail<span class="hl sym">;</span>
    Pair <span class="hl sym">*</span>e<span class="hl sym">;</span>

    list_head<span class="hl sym">.</span>cdr <span class="hl sym">= (</span><span class="hl kwb">unsigned long</span><span class="hl sym">)</span> NULL<span class="hl sym">;</span>
    tail <span class="hl sym">= &amp;</span>list_head<span class="hl sym">;</span>
    <span class="hl kwa">for</span> <span class="hl sym">(;;)</span>
    <span class="hl sym">{</span>
        e <span class="hl sym">=</span> <span class="hl kwd">readexp</span><span class="hl sym">();</span>
        <span class="hl kwa">if</span> <span class="hl sym">(</span> e <span class="hl sym">== (</span>Pair <span class="hl sym">*)</span> EOF <span class="hl sym">)</span>
            <span class="hl kwd">error</span><span class="hl sym">(</span> <span class="hl str">&quot;unexpected EOF in readlist&quot;</span> <span class="hl sym">);</span>
        <span class="hl kwa">else if</span> <span class="hl sym">(</span>e <span class="hl sym">== (</span>Pair <span class="hl sym">*)</span> RIGHT <span class="hl sym">)</span>
            <span class="hl kwa">return</span> list_head<span class="hl sym">.</span>cdr<span class="hl sym">;</span>
        <span class="hl kwa">else</span> <span class="hl sym">{</span>
            <span class="hl com">/* some sort of sexp, either an atom or a list */</span>
            tail<span class="hl sym">-&gt;</span>cdr <span class="hl sym">=</span> <span class="hl kwd">cons</span><span class="hl sym">(</span> e<span class="hl sym">,</span> NULL <span class="hl sym">);</span>
            tail <span class="hl sym">=</span> tail<span class="hl sym">-&gt;</span>cdr<span class="hl sym">;</span>
        <span class="hl sym">}</span>
    <span class="hl sym">}</span>
<span class="hl sym">}</span>
</pre>
</body>
</html>
<!--HTML generated by highlight 2.6.14, http://www.andre-simon.de/-->
